<template>
	<view>
		<navigator class="fenlei-search" open-type="navigate" url="/pages/index/search/index" hover-class="none">
			<view class="fenlei-search-flex">
				<view class="icon icon-sousuo"></view>
				<view>请输入课程的关键字搜索</view>
			</view>
		</navigator>
		<view class="fenlei">
			<view class="fenlei-l">
				<view class="item" :class="{'active':isHot}" @tap="setHot()">热门推荐</view>
				<view v-for="(item,index) in Category" :key="index" 
				:class="{'active' : pid == item.id}" 
				@tap="setCategory(item.id,index)" class="item">
					{{item.name}}
				</view>
			</view>
			<view class="fenlei-r">
				<view class="fenlei-r-nav" v-if="sort_list.length">
					<view class="fenlei-r-nav-flex">
						<view v-for="(item,index) in sort_list" :key="index" :class="{'active' : current_index == index}"  @tap="tabChange(index)" class="item">{{item.name}}</view>
					</view>
				</view>
				<view class="fenlei-ke" :class="{'fenlei-scroll' : list.length > 4 }">
					<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption" :class="{'fenlei-height' : list.length > 4 }">
					<view class="index-content-qy" v-for="(item,index) in list" :key="index" @tap="gotoCourse(item)">
						<image class="video-content-img" :src="item.cover"></image>
						<view class="video-content-flex">
							<view class="video-content-h1">{{item.title}}</view>
							<view class="video-content-learn">
								<view class="video-content-detail">
									<view class="video-content-detail-price video-content-detail-free" v-if="item.price==0.00">免费</view>
									<view class="video-content-detail-price" v-else>￥{{item.price}}</view>
									<view class="video-content-detail-r" v-if="item.price!=0.00">
										<view v-if="item.vip_price==0.00" style="color: #ff4a4a">免费</view>
										<view v-else>￥{{item.vip_price}}</view>
										<image class="video-content-detail-img" :src="static_media.img01"></image>
									</view>
								</view>
							</view>
						</view>
					</view>
					</mescroll-body>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
import { mapState } from 'vuex';
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js'

export default {
	mixins: [MescrollMixin], // 使用mixin
	data() {
		return {
			isHot:true,
			Category:[],
			pid:0,
			sort_list: [],
			current_index: 0,
			
			mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
			// 下拉刷新的配置(可选, 绝大部分情况无需配置)
			downOption: {
				use: false, // 是否启用下拉刷新; 默认true
				auto: true, // 是否在初始化完毕之后自动执行下拉刷新的回调; 默认true
				native: false // 启用系统自带的下拉组件,默认false;仅mescroll-body生效,mescroll-uni无效(native: true, 则需在pages.json中配置"enablePullDownRefresh":true)
			},
			// 上拉加载的配置(可选, 绝大部分情况无需配置)
			upOption: {
				page: {
					num: 0,
					size: 10 // 每页数据的数量,默认10
				},
				noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
				empty: {
					tip: '暂无相关数据'
				}
			},
			// 列表数据
			list: [],
		};
	},
	onShow() {
		this.getCategory();
	},
	onLoad(opts){
		this.pages_params = opts;
	},
	mounted() {},
	computed: {
		...mapState(['userInfo']),
		static_media () {
			return {
				img01: this.$lib.$config.localMedia + '/static/img/vip-price.png',
			}
		},
	},
	components: {
	},
	methods: {
		//进入课程
		gotoCourse(item){
			uni.navigateTo({
			    url: '/pages/index/detail?id=' + item.id
			})
		},
		setHot(){
			this.sort_list = []
			this.pid = 0;
			this.isHot = true;
			this.mescroll.resetUpScroll()
		},
		/*上拉加载的回调*/ //赛事新闻列表
		async upCallback(page,item) {
			let pageNum = page.num; // 页码, 默认从1开始
			let pageSize = page.size; // 页长, 默认每页10条
			
			let data = {}
			if(this.isHot){
				data={}
			}else{
				data={
					page: pageNum,
					limit: pageSize,
					cid:this.cid
				}
			}
				
			let res = await this.$lib.$http.post({
				url: this.isHot ? this.$lib.$urlMap.hotList : this.$lib.$urlMap.courseList,
				data: data,
				needLogin: true
			})
			if (res.code == 1) {
				this.personList = res.data.data;
				// 接口返回的当前页数据列表 (数组)
				let curPageData = res.data.data;
				// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
				let curPageLen = curPageData.length;
				// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
				// let totalPage = data.xxx;
				// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
				let totalSize = res.data.total;
		
				this.mescroll.endBySize(curPageLen, totalSize);
				if (page.num == 1) this.list = []; //如果是第一页需手动置空列表
				this.list = this.list.concat(curPageData); //追加新数据
				/* for (let i = 0, leng = this.personList.length; i < leng; i++) {
					this.personList[i].pay_time = this.$myUtils.formatDate(this.personList[i].pay_time * 1000, 'Y年M月D日 h:m:s')
				} */
			} else {
				this.mescroll.endErr()
			}
			// 此处仍可以继续写其他接口请求...
			// 调用其他方法...
		},
		async getCategory(){
			let res = await this.$lib.$http.post({
				url:this.$lib.$urlMap.category,
				data:{},
				needLogin:true
			});
			if(res.code = 1){
				this.Category = res.data
			}
		},
		setCategory(pid,index){
			this.isHot = false
			this.pid = pid
			if(this.Category[index].child.length > 0){
				this.sort_list = this.Category[index].child
				this.current_index = 0
				this.cid = this.sort_list[0].id
			}else{
				this.sort_list = []
				this.cid = pid
			}
			this.mescroll.resetUpScroll()
		},
		// tab切换
		tabChange(index) {
			this.current_index = index;
			this.cid = this.sort_list[index].id;
			this.courseList = {}
			this.mescroll.resetUpScroll()
		},
	}
};
</script>
<style>
page {
	background: #fff;
}
.fenlei-search{
	background: #fff;
}
.fenlei-search-flex{
	display: flex;
	align-items: center;
	background: #f5f5f5;
	font-size: 13px;
	padding: 9px 16px;
	margin: 11px;
	border-radius: 21px;
	color: #989898;
}
.fenlei-search .icon{
	font-size: 18px;
	margin-right: 5px;
}
.fenlei{
	display: flex;
	align-items: self-start;
	height: calc(100vh - 114px);
	width: 100%;
	overflow: hidden;
}
.fenlei-l{
	background: #f5f5f5;
	height: 100%;
	width: 20%;
    overflow: scroll;
}
.fenlei-l .active{
	background: #fff;
	font-weight: bold;
	border-left: solid 3px #007aff;
	color: #000 !important;
}
.fenlei-l .item{
	padding: 12px 0;
	text-align: center;
	font-size: 14px;
	color: #7d7d7d;
	position: relative;
}
.fenlei-r{
	display: flex;
	flex-direction: column;
	width: 80%;
	height: 100%;
	padding-left: 12px;
	overflow: hidden;
}
.fenlei-ke{
	height: 100%;
}
.fenlei-scroll{overflow-y: scroll;}
.fenlei-height{margin-bottom: -45px;}
.fenlei-r-nav{
	display: flex;
	align-items: center;
	background: #f5f5f5;
	padding: 18px 0;
	overflow-x: scroll;
	width: 100%;
	color: #4a4a4a;
}
.fenlei-r-nav-flex {
	display: flex;
	align-items: center;
	font-size: 13px;
	text-align: center;
	justify-content: center;
}
.fenlei-r-nav .item{
	width: 84px;
	display: flex;
	align-items: center;
	font-size: 13px;
	text-align: center;
	justify-content: center;
	height: 21px;
	text-overflow: ellipsis;
	overflow: hidden;
	display: -webkit-box;
	-webkit-line-clamp: 1;
	-webkit-box-orient: vertical;
	word-break: break-all;
}
.fenlei-r-nav-flex .active{
	color: #2c66f7;
}
.index-content-qy {
	padding-top: 12px;
	width: 100%;
	margin: 0 auto;
	display: flex;
	-webkit-box-pack: justify;
	justify-content: space-between;
	border-bottom: solid 1px #f5f5f5;
	padding-bottom: 12px;
}
.fenlei-ke .index-content-qy:last-child{
	border-bottom: none;
	padding-bottom: 0;
}
.video-content-img {
	width: 97px;
	height: 70px;
	border-radius: 3px;
}
.video-content-flex {
	display: flex;
	flex-direction: column;
	align-items: start;
	flex: 1;
	padding-left: 12px;
	height: 70px;
	justify-content: space-between;
}
.video-content-h1 {
	color: #484848;
	font-size: 13px;
	line-height: 16px;
	margin-right: 4px;
	overflow: hidden;
	-o-text-overflow: ellipsis;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp:2;
	/* autoprefixer: off */
	-webkit-box-orient: vertical;
	/* autoprefixer: on */
	word-break: break-all
}
.video-content-learn {
	display: flex;
	justify-content: space-between;
	width: 92%;
	align-items: center;
}
</style>
